//
//  ScoreCellView.m
//  Quiz
//
//  Created by AnhLH-Oriolus on 31/07/2011.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ScoreCellView.h"
#import "FBConnect.h"
#import "countryquizAppDelegate.h"


static NSString* kApiKey = @"257288537619170";

// Enter either your API secret or a callback URL (as described in documentation):
static NSString* kApiSecret = @"2fb4771c5662cda7fa8f1a1f7b3daebd"; // @"<YOUR SECRET KEY>";
static NSString* kGetSessionProxy = nil; // @"<YOUR SESSION CALLBACK)>";

@implementation ScoreCellView


-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier withMode:(int)mode{
	iMode = mode;
	
	if((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])){
		if (kGetSessionProxy) {
			_session = [[FBSession sessionForApplication:kApiKey getSessionProxy:kGetSessionProxy
												delegate:self] retain];
		} else {
			_session = [[FBSession sessionForApplication:kApiKey secret:kApiSecret delegate:self] retain];
		}
		
		
		lblPlayerName = [[FontLabel alloc] initWithFrame:CGRectMake(10.0, 0, 100, 40) fontName:@"scoreboard" pointSize:15];
		[lblPlayerName setBackgroundColor:[UIColor clearColor]];
		lblPlayerName.textAlignment = UITextAlignmentLeft;
		lblPlayerName.textColor = [UIColor colorWithRed:243 green:255 blue:62 alpha:1.0];
		[self.contentView addSubview:lblPlayerName];
		[lblPlayerName release];
		
		lblScore = [[FontLabel alloc] initWithFrame:CGRectMake(110, 0, 100, 40) fontName:@"scoreboard" pointSize:15];
		[lblScore setBackgroundColor:[UIColor clearColor]];
		lblScore.textAlignment = UITextAlignmentCenter;
		lblScore.textColor = [UIColor colorWithRed:243 green:255 blue:62 alpha:1.0];
		[self.contentView addSubview:lblScore];
		[lblScore release];
		
		UIImageView *imgLayoutBtn = [[UIImageView alloc] initWithFrame:CGRectMake(220, 0, 80, 30)];
		imgLayoutBtn.image = [UIImage imageNamed:@"images/buttonUpload.png"];
		[self.contentView addSubview:imgLayoutBtn];
		[imgLayoutBtn release];
		lblUpload = [[UIButton alloc]initWithFrame:CGRectMake(220, 0, 80, 40)];
		[lblUpload setBackgroundColor:[UIColor clearColor]];
		[lblUpload addTarget:self action:@selector(uploadScore:) forControlEvents:UIControlEventTouchUpInside];
		[self.contentView addSubview:lblUpload];
		[lblUpload release];

	}
	return self;
}

- (void)layoutSubviews {
	[super layoutSubviews];
	
    CGRect contentRect = self.contentView.bounds;
	
	lblPlayerName.frame = CGRectMake(10.0, 0, 100, 40);
	lblScore.frame = CGRectMake(110, 0, 100, 40);
	lblUpload.frame = CGRectMake(210, 0, 80, 40);
}

-(void)uploadScore:(id)sender{
	NSLog(@"Up");
	if (_session.isConnected) {
		[_session logout];
	} else {
		FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:_session] autorelease];
		[dialog show];
	}
}

-(void)postToWall{
	//post to wall
	NSString *caption = [NSString stringWithFormat:@"I have got %@ points in quizfootball app", lblScore.text];
	NSString *descript=@"";
	
	FBStreamDialog *dialog = [[[FBStreamDialog alloc] init] autorelease];
	dialog.userMessagePrompt = [NSString stringWithFormat:@"Write comment here to post to your wall!"];
	dialog.attachment = [NSString stringWithFormat:@"{\"name\":\"Facebook Connect for iPhone\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone\",\"caption\":\"%@\",\"description\":\"%@\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}", caption, descript];
	
	
	[dialog show];
}

-(void)setPlayerName:(NSString *)playerName withScore:(int)score{
	lblPlayerName.text = playerName;
	lblScore.text = [NSString stringWithFormat:@"%d",score];
}

- (void)viewDidLoad {
	[_session resume];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// FBDialogDelegate

- (void)dialog:(FBDialog*)dialog didFailWithError:(NSError*)error {
	//_label.text = [NSString stringWithFormat:@"Error(%d) %@", error.code,
	//			   error.localizedDescription];
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// FBSessionDelegate

- (void)session:(FBSession*)session didLogin:(FBUID)uid {

	NSString* fql = [NSString stringWithFormat:
					 @"select uid,name from user where uid == %lld", session.uid];
	
	NSDictionary* params = [NSDictionary dictionaryWithObject:fql forKey:@"query"];
	[[FBRequest requestWithDelegate:self] call:@"facebook.fql.query" params:params];
}

- (void)sessionDidNotLogin:(FBSession*)session {
	//_label.text = @"Canceled login";
}

- (void)sessionDidLogout:(FBSession*)session {
	//_label.text = @"Disconnected";
//	_permissionButton.hidden = YES;
//	_feedButton.hidden       = YES;
//	_statusButton.hidden     = YES;
//	_photoButton.hidden      = YES;
}

///////////////////////////////////////////////////////////////////////////////////////////////////
// FBRequestDelegate

- (void)request:(FBRequest*)request didLoad:(id)result {
	if ([request.method isEqualToString:@"facebook.fql.query"]) {
		//NSArray* users = result;
//		NSDictionary* user = [users objectAtIndex:0];
//		NSString* name = [user objectForKey:@"name"];
		[self postToWall];
		//_label.text = [NSString stringWithFormat:@"Logged in as %@", name];
	}
}

- (void)request:(FBRequest*)request didFailWithError:(NSError*)error {
	//_label.text = [NSString stringWithFormat:@"Error(%d) %@", error.code,
	//			   error.localizedDescription];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
	[_session release];
    [super dealloc];
}


@end
